package com.LatitudeYao;

/**
 * Created by GTS on 2017/7/13.
 * 饿汉模式
 */
class SingltonDemo{
    private static SingltonDemo singltonDemo = null;//初始化静态成员引用
    private SingltonDemo(){}
    public synchronized static SingltonDemo getInstance(){//保证线程安全
        if(singltonDemo == null){
            singltonDemo = new SingltonDemo();
        }
        return  singltonDemo;
    }
}


/*


 */
class SingletonClass {
     private static final SingletonClass instance=new SingletonClass();//直接静态初始化，保证只有一个
     //私有构造函数
     private SingletonClass() {}
     public static SingletonClass getInstance() {
         return instance;
     }
}


public class MySinglton {
    public static void main(String[] args) {
        SingltonDemo demo1 = SingltonDemo.getInstance();
        SingltonDemo demo2 = SingltonDemo.getInstance();
        System.out.println("demo1 ==demo2  ?? "+(demo1 == demo2));


    }
}
